using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using digit.Core;

namespace SMS.SportService
{
    [WebService(Namespace = "http://203.162.71.213:8092/SportServiceReceiver")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class SportAutoService : System.Web.Services.WebService
    {
        public AuthHeader Authentication;

        public SportAutoService() { }

        public class AuthHeader : SoapHeader
        {
            public string Username;
            public string Password;
        }

        //[SoapHeader("Authentication")]
        [WebMethod]
        public long SportServiceReceiver(long contentId, string serviceCode, string cupCode, string matchCode, string homeCode, string awayCode, string msg)
        {
            //if (Authenticated())
            //{
                ServiceSportsInfo objInfo = new ServiceSportsInfo();
                long serId = 0;

                if (serviceCode.Replace(" ", "").Trim() != "")
                {
                    if (serviceCode.Trim().ToLower() == "tyle")
                    {
                        objInfo.Cmd1 = Util.GetAppSettings("BET");
                        objInfo.Cmd2 = "IMP";
                        objInfo.Code1 = Util.GetAppSettings("TYLE");
                        objInfo.Code2 = "TYLE";
                        objInfo.Param1 = homeCode;
                        objInfo.Param2 = awayCode;
                        objInfo.Param3 = objInfo.Cmd1 + "_" + objInfo.Code1 + "_" + homeCode;
                        objInfo.Param4 = "BET_" + "TL_" + awayCode;
                        objInfo.ServiceID = "6298";
                    }

                    else if (serviceCode.Trim().ToLower() == "ketqua")
                    {
                        if (cupCode.Replace(" ", "") != "")
                        {
                            objInfo.Cmd1 = Util.GetAppSettings("BET");
                            objInfo.Cmd2 = "IMP";
                            objInfo.Code1 = Util.GetAppSettings("KETQUA");
                            objInfo.Code2 = "KETQUA";
                            objInfo.Param1 = cupCode;
                            objInfo.Param3 = objInfo.Cmd1 + "_" + objInfo.Code1 + "_" + cupCode;
                            objInfo.ServiceID = "6298";
                        }
                        else
                        {
                            objInfo.Cmd1 = Util.GetAppSettings("BET");
                            objInfo.Cmd2 = "IMP";
                            objInfo.Code1 = Util.GetAppSettings("KETQUA");
                            objInfo.Code2 = "KETQUA";
                            objInfo.Param1 = homeCode;
                            objInfo.Param2 = awayCode;
                            objInfo.Param3 = objInfo.Cmd1 + "_" + objInfo.Code1 + "_" + homeCode;
                            objInfo.Param4 = objInfo.Cmd1 + "_" + objInfo.Code1 + "_" + awayCode;
                            objInfo.ServiceID = "6598";
                        }
                    }
                    else return 0;

                    string[] values = msg.ToString().Split("$$".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                    string[] sContents = new string[5];
                    int iCont = 0;

                    for (int i = 0; i < values.Length; i++)
                    {
                        if (!values[i].ToString().Replace(" ", "").Equals(""))
                        {
                            sContents[iCont] = values[i].ToString();
                            iCont++;
                        }
                    }

                    objInfo.Content1 = sContents[0];
                    objInfo.Content2 = sContents[1];
                    objInfo.Content3 = sContents[2];
                    objInfo.Content4 = sContents[3];
                    objInfo.Content5 = sContents[4];

                    byte j = 0;

                    if (objInfo.Content1 != null && objInfo.Content1.Replace(" ", "") != "") j++;

                    if (objInfo.Content2 != null && objInfo.Content2.Replace(" ", "") != "") j++;

                    if (objInfo.Content3 != null && objInfo.Content3.Replace(" ", "") != "") j++;

                    if (objInfo.Content4 != null && objInfo.Content4.Replace(" ", "") != "") j++;

                    if (objInfo.Content5 != null && objInfo.Content5.Replace(" ", "") != "") j++;                       

                    objInfo.NumOfMsg = j;

                    objInfo.ContentType = 0;
                    objInfo.MsgType = 1;
                    objInfo.CreatedBy = "AutoUpdate";
                    objInfo.CreatedDate = DateTime.Now;
                    objInfo.Status = 1;

                    try
                    {
                        if (contentId != -1 && contentId != 0)
                        {
                            objInfo.ServiceSportID = contentId;
                            bool result = ServiceSportsManager.Update(objInfo);
                            if (result) return contentId;
                            else return -1;
                        }
                        else
                            serId = ServiceSportsManager.Insert(objInfo);
                    }
                    catch { return -1; }
                }

                if (serId != 0) return serId;
                else
                    return -1;
            //}
            //else
            //    return 98;
        }

        //[SoapHeader("Authentication")]
        [WebMethod]
        public long LotteryServiceReceiver(long contentId, string serviceId, string serviceCode, string sub1, string sub2, string msg)
        {
            //if (Authenticated())
            //{
            ServiceDetailsInfo objInfo = new ServiceDetailsInfo();
            long serId = 0;

            if (serviceCode.Replace(" ", "").Trim() != "")
            {
                if (serviceCode.Trim().ToLower() == "xs")
                {
                    objInfo.Cmd1 = serviceCode;
                    objInfo.Cmd2 = "IMP";
                    objInfo.Code1 = sub1;
                    objInfo.Code2 = "KQ";                    
                    objInfo.Param1 = sub2;
                    objInfo.Param2 = sub2;
                    objInfo.Param3 = objInfo.Cmd1 + "_" + objInfo.Code1 + "_" + sub2;
                    objInfo.Param4 = serviceCode + "_" + sub1 + "_" + sub2;
                    objInfo.ServiceID = serviceId;

                    objInfo.Content1 = msg.ToString();

                    objInfo.NumOfMsg = (byte)1;

                    objInfo.ContentType = (byte)0;
                    objInfo.MsgType = (byte)1;
                    objInfo.CreatedBy = "AutoUpdate";
                    objInfo.CreatedDate = DateTime.Now;
                    objInfo.Status = (byte)1;

                    try
                    {
                        if (contentId != -1 && contentId != 0)
                        {
                            objInfo.ServiceDetailID = contentId;
                            bool result = ServiceDetailsManager.Update(objInfo);
                            if (result) return contentId;
                            else return -1;
                        }
                        else
                            serId = ServiceDetailsManager.Insert(objInfo);
                    }
                    catch { return -1; }
                }
                else return 0;
            }

            if (serId != 0) return serId;
            else
                return -1;
            //}
            //else
            //    return 98;
        }

        private bool Authenticated()
        {
            if (Authentication.Username == "sportauto" && Authentication.Password == "isvalid") return true;
            else
                return false;
        }

    }
}
